package com.zyl.Ecs.common.domain.auth.vo;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.zyl.Ecs.common.domain.auth.ext.RoleExt;
import com.zyl.Ecs.common.utils.ObjectUtil;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @version v1.0
 * @ProjectName: Ecs
 * @Package: com.zyl.Ecs.auth.domain.vo
 * @ClassName: RoleVO
 * @Description TODO (角色VO)
 * @Author 翟玉龙
 * @Date 2024/12/4-16:57
 */
@Data
@Builder
@ToString
public class RoleVO {

    @JsonProperty(value = "id")
    private Integer id;

    @JsonProperty(value = "name")
    private String name;

    @JsonProperty(value = "permissions")
    private List<PermissionVO> permissions;

    @JsonProperty(value = "permissionIds")
    private List<Integer> permissionIds;

    public static RoleVO toVO(RoleExt roleExt) {
        if (ObjectUtil.isNull(roleExt)) {
            return null;
        }
        return RoleVO.builder()
                .id(roleExt.getId())
                .name(roleExt.getName())
                .permissionIds(roleExt.getPermissionIds())
                .permissions(Optional.ofNullable(roleExt.getPermissions())
                        .map(list -> list.stream()
                                .map(PermissionVO::toVO)
                                .collect(Collectors.toList()))
                        .orElse(null))
                .build();
    }
}
